class Army < ActiveRecord::Base
  belongs_to :hero
  belongs_to :attack, :conditions => ['isPegasus = ?', false]
  has_many :troops, :dependent => :destroy, :as => :troopable
  has_many :units, :through => :troops

  def to_s
    hero.name
  end

  def sum_angriff
    sum = 0
    troops.each do |troop|
      sum = sum + troop.angriff
    end
    sum
  end

  def sum_verteidigung
    sum = 0
    troops.each do |troop|
      sum = sum + troop.verteidigung
    end
    sum
  end

  def sum_leben
    sum = 0
    troops.each do |troop|
      sum = sum + troop.leben
    end
    sum
  end

  def sum_punkte
    sum = 0
    troops.each do |troop|
      sum = sum + troop.punkte
    end
    sum
  end

  def verluste_essenzen
    sum = 0
    troops.each do |loss|
      sum = sum + loss.verluste_essenzen
    end
    sum
  end

  def verluste_kristalle
    sum = 0
    troops.each do |loss|
      sum = sum + loss.verluste_kristalle
    end
    sum
  end

  def verluste_kraftsteine
    sum = 0
    troops.each do |loss|
      sum = sum + loss.verluste_kraftsteine
    end
    sum
  end

  def verluste_goldharz
    sum = 0
    troops.each do |loss|
      sum = sum + loss.verluste_goldharz
    end
    sum
  end

  def verluste_punkte
    sum = 0
    troops.each do |loss|
      sum = sum + loss.verluste_punkte
    end
    sum
  end

  def gewinn_essenzen
    return 0 if essenzen == nil
    essenzen - verluste_essenzen
  end

  def gewinn_kristalle
    return 0 if kristalle == nil
    kristalle - verluste_kristalle
  end

  def gewinn_kraftsteine
    return 0 if kraftsteine == nil
    kraftsteine - verluste_kraftsteine
  end

  def gewinn_goldharz
    return 0 if goldharz == nil
    goldharz - verluste_goldharz
  end
end
